Skip to contentMethod: getType(ELContext, Object, Object)
1: /*
2: * Copyright (c) 2023 Contributors to the Eclipse Foundation.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16: package jakarta.servlet.jsp.el;
17:
18: import jakarta.el.ELContext;
19: import jakarta.el.ELClass;
20: import jakarta.el.ELResolver;
21: import jakarta.el.ImportHandler;
22: import jakarta.el.ELException;
23:
24: /**
25: * Defines variable resolution behavior for Class imports and static imports.
26: *
27: * @since JSP 3.1
28: */
29: public class ImportELResolver extends ELResolver {
30:
31: /**
32: * If the base object is <code>null</code>, searches the Class and static imports for an import with the given name
33: * and returns it if an import exists with the given name.
34: *
35: * <p>
36: * The <code>propertyResolved</code> property of the <code>ELContext</code> object must be set to <code>true</code>
37: * by this resolver before returning if an import is matched. If this property is not <code>true</code> after this
38: * method is called, the caller should ignore the return value.
39: * </p>
40: *
41: * @param context The context of this evaluation.
42: * @param base Only <code>null</code> is handled by this resolver. Other values will result in an immediate
43: * return.
44: * @param property The name of the import to resolve.
45: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>,
46: * then the import; otherwise undefined.
47: * @throws NullPointerException if context is <code>null</code>
48: * @throws ELException if an exception was thrown while performing the property or variable resolution. The
49: * thrown exception must be included as the cause property of this exception, if
50: * available.
51: */
52: @Override
53: public Object getValue(ELContext context, Object base, Object property) {
54:
55: if (context == null) {
56: throw new NullPointerException();
57: }
58:
59: ImportHandler importHandler = context.getImportHandler();
60: if (base == null && property instanceof String && importHandler != null) {
61: String attribute = (String) property;
62: Object value = null;
63: // Check to see if the property is an imported class
64: Class<?> c = importHandler.resolveClass(attribute);
65: if (c != null) {
66: value = new ELClass(c);
67: // A possible optimization is to set the ELClass
68: // instance in an attribute map.
69: }
70: // Check to see if the property is an imported static field
71: if (value == null) {
72: c = importHandler.resolveStatic(attribute);
73: if (c != null) {
74: try {
75: value = c.getField(attribute).get(null);
76: } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
77: SecurityException e) {
78: // Most (all?) of these should have been
79: // prevented by the checks when the import
80: // was defined.
81: }
82: }
83: }
84: if (value != null) {
85: context.setPropertyResolved(true);
86: }
87: return value;
88: }
89: return null;
90: }
91:
92: /**
93: * Always returns {@code null} since in normal usage {@link ScopedAttributeELResolver} will handle calls to
94: * {@link ELResolver#getType(ELContext, Object, Object)}.
95: *
96: * @param context The context of this evaluation.
97: * @param base Ignored
98: * @param property Ignored
99: * @return Always {@code null}
100: * @throws NullPointerException if context is <code>null</code>
101: * @throws ELException if an exception was thrown while performing the property or variable resolution. The
102: * thrown exception must be included as the cause property of this exception, if
103: * available.
104: */
105: @Override
106: public Class<Object> getType(ELContext context, Object base, Object property) {
107:
108:• if (context == null) {
109: throw new NullPointerException();
110: }
111:
112: return null;
113: }
114:
115: /**
116: * Always a NO-OP since in normal usage {@link ScopedAttributeELResolver} will handle calls to
117: * {@link ELResolver#setValue(ELContext, Object, Object, Object)}.
118: *
119: * @param context The context of this evaluation.
120: * @param base Ignored
121: * @param property Ignored
122: * @param val Ignored
123: * @throws NullPointerException if context is <code>null</code>.
124: * @throws ELException if an exception was thrown while performing the property or variable resolution. The
125: * thrown exception must be included as the cause property of this exception, if
126: * available.
127: */
128: @Override
129: public void setValue(ELContext context, Object base, Object property, Object val) {
130: if (context == null) {
131: throw new NullPointerException();
132: }
133: }
134:
135: /**
136: * Always returns {@code false} since in normal usage {@link ScopedAttributeELResolver} will handle calls to
137: * {@link ELResolver#isReadOnly(ELContext, Object, Object)}.
138: *
139: * @param context The context of this evaluation.
140: * @param base Ignored
141: * @param property Ignored
142: * @return Always {@code false}
143: * @throws NullPointerException if context is <code>null</code>.
144: * @throws ELException if an exception was thrown while performing the property or variable resolution. The
145: * thrown exception must be included as the cause property of this exception, if
146: * available.
147: */
148: @Override
149: public boolean isReadOnly(ELContext context, Object base, Object property) {
150: if (context == null) {
151: throw new NullPointerException();
152: }
153: return false;
154: }
155:
156: /**
157: * Always returns {@code null} since in normal usage {@link ScopedAttributeELResolver} will handle calls to
158: * {@link ELResolver#getCommonPropertyType(ELContext, Object)}.
159: *
160: * @param context Ignored
161: * @param base Ignored
162: *
163: * @return Always {@code null}
164: */
165: @Override
166: public Class<String> getCommonPropertyType(ELContext context, Object base) {
167: return null;
168: }
169: }